package co.baiku.boot.security.handler;

import co.baiku.boot.common.message.Message;
import co.baiku.boot.common.tools.ServletTools;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限不足处理类
 */
@Component("RestAuthenticationAccessDeniedHandler")
public class RestAuthenticationAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, AccessDeniedException e) {
        String body = Message.fail(HttpStatus.FORBIDDEN, e.getMessage()).json();
        ServletTools.printJson(response, body);
    }
}
